package com.taijihuabao.common.activititasklistener.reissuecardtasklistener;

import com.taijihuabao.modules.entity.ApprovalEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.enums.optenums.dept.DeptRoleEnum;
import com.taijihuabao.modules.mapper.ApprovalMapper;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 考勤专员会签人员
 *
 * @author Tu
 * @date 2022/1/13 17:08
 */
@Component
public class CheckInCountersignListener implements ExecutionListener {

    @Resource
    private ApprovalMapper approvalMapper;
    @Resource
    private DeptUserMapper deptUserMapper;

    @Override
    public void notify(DelegateExecution execution) {
        //获取businessKey
        String businessKey = execution.getProcessInstanceBusinessKey();
        //获取审批数据
        ApprovalEntity approvalEntity = approvalMapper.selectById(Long.valueOf(businessKey));
        //查询部门
        List<DeptUserEntity> deptUserEntities = deptUserMapper.selectDeptByDeptRole(DeptRoleEnum.CheckInCommissioner.getCode());
        //遍历部门人员查出考勤专员
        List<String> checkInList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(deptUserEntities)) {
            for (DeptUserEntity due : deptUserEntities) {
                if (DeptRoleEnum.CheckInCommissioner.getCode().equals(due.getDeptRole())) {
                    //设置会签人员
                    checkInList.add(String.valueOf(due.getUserId()));
                }
            }
        } else {
            //无考勤人员设置上一节点审批人
            checkInList.add(String.valueOf(approvalEntity.getApprovalUserId()));
        }
        execution.setVariable("checkInList", checkInList);
    }

}
